package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppNewsPush;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserNewsSwitch;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppUserNewsSwitchMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.service.DankMerchantsDefBusinessService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNewsPushService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserNewsSwitchService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.databankUser.MessageSettingRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userNewsSwitch.SetRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.BaseRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.dxshAppNews.SettingStatusVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 电享生活app消息表 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-05-14
 */
@Service
public class DxshAppUserNewsSwitchServiceImpl extends ServiceImpl<DxshAppUserNewsSwitchMapper, DxshAppUserNewsSwitch> implements DxshAppUserNewsSwitchService {

    public static Map<Integer,String> msgTypeMap = new HashMap<>();

    public static Map<String,String> sonMsgTypeMap = new HashMap<>();

    static {
        msgTypeMap.put(1,"订单动态");
        msgTypeMap.put(2,"营销实况");
        msgTypeMap.put(3,"其他通知");

        sonMsgTypeMap.put("1_1", "小程序订单");
        sonMsgTypeMap.put("1_2", "觅东东订单");
        sonMsgTypeMap.put("3_1", "小程序通知");
        sonMsgTypeMap.put("3_2", "觅东东通知");
        sonMsgTypeMap.put("3_3", "客户动态");
        sonMsgTypeMap.put("3_4", "社群消息");
        sonMsgTypeMap.put("3_5", "系统通知");
    }

    @Autowired
    DxshAppUserNewsSwitchMapper dxshAppUserNewsSwitchMapper;

    @Autowired
    DxshAppNewsPushService dxshAppNewsPushService;

    @Autowired
    DankMerchantsDefBusinessService dankMerchantsDefBusinessService;


    @Override
    public Map<String, Object> set(BaseRqVo baseRqVo) {
        Integer userId = baseRqVo.getUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitches = dxshAppUserNewsSwitchMapper.selectList(queryWrapper);
        dxshAppUserNewsSwitches = this.fillingNewTypes(dxshAppUserNewsSwitches,userId);
        this.saveOrUpdateBatch(dxshAppUserNewsSwitches);
        List<SetRpVo> setRpVos = BeanUtils.copyList(dxshAppUserNewsSwitches,SetRpVo.class);
        Collections.sort(setRpVos, new Comparator<SetRpVo>() {
            @Override
            public int compare(SetRpVo o1, SetRpVo o2) {
                if(o1.getParentType() > o2.getParentType()) {
                    return 1;
                }
                return -1;
            }
        });
        return ResultMapHelper.success(setRpVos, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> openClose(Integer id, Integer isOpen) {
        DxshAppUserNewsSwitch dxshAppUserNewsSwitch = dxshAppUserNewsSwitchMapper.selectById(id);
        if(dxshAppUserNewsSwitch == null){
            return ResultMapHelper.success(ResultMsg.HAND_SUCCESS);
        }
        dxshAppUserNewsSwitch.setIsOpen(isOpen);
        dxshAppUserNewsSwitchMapper.updateById(dxshAppUserNewsSwitch);
        return ResultMapHelper.success(ResultMsg.HAND_SUCCESS);
    }

    private List<DxshAppUserNewsSwitch> fillingNewTypes(List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitches, Integer userId) {
        if(dxshAppUserNewsSwitches != null){
            Map<Integer,DxshAppUserNewsSwitch> dxshAppUserNewsSwitchMap = new HashMap<>();
            for(DxshAppUserNewsSwitch dxshAppUserNewsSwitch : dxshAppUserNewsSwitches){
                dxshAppUserNewsSwitchMap.put(dxshAppUserNewsSwitch.getParentType(),dxshAppUserNewsSwitch);
            }
            if(DxshAppNewsServiceImpl.APP_NEW_TYPE.size() > 0){
                for(Map.Entry<Integer,Integer> newsType : DxshAppNewsServiceImpl.APP_NEW_TYPE.entrySet()){
                    if(!dxshAppUserNewsSwitchMap.containsKey(newsType.getKey())){
                        DxshAppUserNewsSwitch dxshAppUserNewsSwitch = new DxshAppUserNewsSwitch();
                        dxshAppUserNewsSwitch.setUserId(userId);
                        dxshAppUserNewsSwitch.setParentType(newsType.getKey());
                        dxshAppUserNewsSwitch.setNewTime(LocalDateTime.now());
                        dxshAppUserNewsSwitches.add(dxshAppUserNewsSwitch);
                    }
                }
            }
        }
        return dxshAppUserNewsSwitches;
    }

    @Override
    public Set<Integer> getNewsSwitchByUserId(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
        queryWrapper.eq(DxshAppUserNewsSwitch.IS_OPEN, Constants.COMMON_FLAG_TRUE_INT);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitches = dxshAppUserNewsSwitchMapper.selectList(queryWrapper);
        Set<Integer> types = dxshAppUserNewsSwitches.stream().map(DxshAppUserNewsSwitch::getParentType).collect(Collectors.toSet());
        return types;
    }


    public Set<Integer> getNewsSwitchByUserId_v2(Integer userId,Integer messageType,Integer secondMessageType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.MESSAGE_TYPE,messageType);
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitch = dxshAppUserNewsSwitchMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(dxshAppUserNewsSwitch)){
           return new HashSet<>();
        }
        Integer messageTypeId = dxshAppUserNewsSwitch.get(0).getId();
        queryWrapper.clear();
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
        queryWrapper.eq(DxshAppUserNewsSwitch.FIRST_MESSAGE_TYPE_ID,messageTypeId);
        queryWrapper.eq(DxshAppUserNewsSwitch.SECOND_MESSAGE_TYPE,secondMessageType);
        queryWrapper.eq(DxshAppUserNewsSwitch.IS_OPEN, Constants.COMMON_FLAG_TRUE_INT);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitches = dxshAppUserNewsSwitchMapper.selectList(queryWrapper);
        Set<Integer> types = dxshAppUserNewsSwitches.stream().map(DxshAppUserNewsSwitch::getSecondMessageType).collect(Collectors.toSet());
        return types;
    }

    @Override
    public Map<String, Object> getMyMessageSettings(Integer userId) {
        /*Integer chooseBusinessId = dankMerchantsDefBusinessService.getChooseBusinessId(userId);
        if(chooseBusinessId == null){
            return ResultMapHelper.failure("请选择店铺");
        }*/
        Map<Integer,Object> saveVoMap = new HashMap<>();
        MessageSettingRpVo messageSettingRpVo = null;
        List<MessageSettingRpVo> messageSettingRpVoList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.USER_ID,userId);
//        queryWrapper.eq(DxshAppUserNewsSwitch.BUSINESS_ID,chooseBusinessId);
        List<DxshAppUserNewsSwitch> dxshAppUserNewsSwitchList = dxshAppUserNewsSwitchMapper.selectList(queryWrapper);
        for(DxshAppUserNewsSwitch d : dxshAppUserNewsSwitchList){
            if(d.getMessageType() != null){
                messageSettingRpVo = new MessageSettingRpVo();
                messageSettingRpVo.setId(d.getId());
                messageSettingRpVo.setIsOpen(d.getIsOpen());
                messageSettingRpVo.setSetName(msgTypeMap.get(d.getMessageType()));
                messageSettingRpVo.setFirstMessageType(d.getMessageType());
                messageSettingRpVoList.add(messageSettingRpVo);
                saveVoMap.put(d.getId(),messageSettingRpVo);
            }else{
                if(saveVoMap.size() != 0){
                    MessageSettingRpVo m = (MessageSettingRpVo)saveVoMap.get(d.getFirstMessageTypeId());
                    List<MessageSettingRpVo> sonSetting = m.getSonSetting();
                    MessageSettingRpVo sonMsg = new MessageSettingRpVo();
                    sonMsg.setId(d.getId());
                    String sonMsgType = m.getFirstMessageType().toString()+"_" +d.getSecondMessageType().toString();
                    sonMsg.setSetName(sonMsgTypeMap.get(sonMsgType));
                    sonMsg.setIsOpen(d.getIsOpen());
                    if(sonSetting == null){
                        sonSetting = new ArrayList<>();
                    }
                    sonSetting.add(sonMsg);
                    m.setSonSetting(sonSetting);
                }
            }
        }
        DxshAppNewsPush dxshAppNewsPush = dxshAppNewsPushService.findByUserId(userId);
        Map<String,Object> userData = new HashMap<>();
        if(dxshAppNewsPush == null){
            userData.put("pushTime", DxshAppNewsPushService.PUSH_TIME);
            dxshAppNewsPushService.addPushTime(DxshAppNewsPushService.PUSH_TIME,userId);
        }else{
            userData.put("pushTime",dxshAppNewsPush.getPushTime());
        }
        return ResultMapHelper.success(messageSettingRpVoList,userData,ResultMsg.HAND_SUCCESS);
    }

    @Override
    public List<DxshAppUserNewsSwitch> getChildType(Integer firstId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.FIRST_MESSAGE_TYPE_ID,firstId);
        return dxshAppUserNewsSwitchMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> changeSettingStatus(SettingStatusVo settingStatusVo) {
        Integer turnStatus = settingStatusVo.getTurnStatus();
        DxshAppUserNewsSwitch dxshAppUserNewsSwitch = findById(settingStatusVo.getSetId());
        if(dxshAppUserNewsSwitch.getMessageType() != null){
            if(turnStatus != null){
                dxshAppUserNewsSwitch.setIsOpen(turnStatus);
            }else{
                dxshAppUserNewsSwitch.setIsOpen(dxshAppUserNewsSwitch.getIsOpen() == 1?0:1);
            }
            List<DxshAppUserNewsSwitch> childType = getChildType(settingStatusVo.getSetId());
            for(DxshAppUserNewsSwitch d :childType){
                d.setIsOpen(dxshAppUserNewsSwitch.getIsOpen() == 1?1:0);
                dxshAppUserNewsSwitchMapper.updateById(d);
            }
        }else{
            if(turnStatus != null){
                dxshAppUserNewsSwitch.setIsOpen(turnStatus);
            }else{
                dxshAppUserNewsSwitch.setIsOpen(dxshAppUserNewsSwitch.getIsOpen() == 1?0:1);
            }
        }
        dxshAppUserNewsSwitchMapper.updateById(dxshAppUserNewsSwitch);
        return ResultMapHelper.success(null,ResultMsg.HAND_SUCCESS);
    }

    @Override
    public DxshAppUserNewsSwitch findById(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserNewsSwitch.ID,id);
        return dxshAppUserNewsSwitchMapper.selectOne(queryWrapper);
    }
}
